﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using WebResource.Zip;
using System.Text.RegularExpressions;


public partial class Zip_Index : System.Web.UI.Page
{
    private string ZIPROOT = "/Zip/Zip.aspx";
    private string MERGERROOT = "/Zip/Merger.aspx";
    private string ROOTPATH;
    private MyTreeNode ROOT;
    public string zipHTML = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        ROOTPATH = Server.MapPath("~");
        ROOT = GetNode(ROOTPATH,true);
        ROOT.SubNodeList.RemoveAll(delegate(MyTreeNode _node)
        {
            Regex reg = new Regex("(App_Data|bin|Zip|obj|Properties|zhaogangHTML)$");
            return reg.IsMatch(_node.PathName);
            //return GetShortName(_node.PathName).StartsWith("min");
        });
        zipHTML = GetHtml(ROOT);
    }

    public string GetHtml(MyTreeNode root)
    {
        StringBuilder sbZip = new StringBuilder();
        StringBuilder sbMerge = new StringBuilder();
        sbZip.Append("<h3>压缩地址：</h3><ul>");
        sbMerge.Append("<h3>合并地址：</h3><ul>");
        if (root.SubNodeList != null && root.SubNodeList.Count > 0)
        {
            foreach (MyTreeNode _subNode in root.SubNodeList)
            {
                sbZip.AppendFormat("<li><a href='{0}?path={1}'>{1}</a></li>", ZIPROOT, GetShortName(_subNode.PathName));
                sbMerge.AppendFormat("<li><a href='{0}?path={1}'>{1}</a></li>",MERGERROOT, GetShortName(_subNode.PathName));
            }
        }
        sbZip.Append("</ul>");
        sbMerge.Append("</ul>");
        return sbZip.Append(sbMerge.ToString()).ToString();
    }

    public MyTreeNode GetNode(string root,bool flag)
    {
        MyTreeNode thisNode = new MyTreeNode();
        thisNode.PathName = root;
        thisNode.FileList = Directory.GetFiles(root, "*.js", SearchOption.TopDirectoryOnly);
        string[] _sub = Directory.GetDirectories(root);
        if (_sub.Length > 0 && flag == true)
        {
            thisNode.SubNodeList = new List<MyTreeNode>();
            foreach (string _subroot in _sub)
            {
                thisNode.SubNodeList.Add(GetNode(_subroot, false));
            }
        }
        return thisNode;
    }

    public string GetShortName(string path)
    {
        string[] _temp = path.Split('\\');
        if (_temp == null || _temp.Length == 0) return string.Empty;
        else return _temp[_temp.Length - 1];
    }
    
}